﻿Public Class frmLettresType

    Private szSQL As String
    Private rs As ADODB.Recordset = Nothing

    Private Sub frmLettresType_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        Call InitListeLettres(LV_LETTRES, 0)
        ' Cacher les détails
        TC_Details.Visible = False

        ' utilisateur autorisé à modifier ?
        If gUserAcces < 9 Then
            BO_ENREGISTRER.Enabled = False
        Else
            BO_ENREGISTRER.Enabled = True
        End If

    End Sub

    Private Sub BO_ENREGISTRER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ENREGISTRER.Click
        If TE_REF.TextLength = 0 Then
            MsgBox("Veuillez saisir la référence de la lettre")
            Exit Sub
        End If

        If TE_LIBELLE.TextLength = 0 Then
            MsgBox("Veuillez saisir la désignation de la lettre")
            Exit Sub
        End If

        szSQL = "CALL spUPD_LETTRE('" & TE_REF.Text & "','" & nq(TE_LIBELLE.Text) & "','" & nq(TE_CORPS.Text) & "');"
        ExecQuery(ctDSN_DEVFAC, szSQL)

        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

    Private Sub BO_ANNULER_Click(sender As System.Object, e As System.EventArgs) Handles BO_ANNULER.Click
        ' Cacher les détails
        TC_Details.Visible = False
        ' réaffiche la viewlist
        InitListeLettres(LV_LETTRES, LV_LETTRES.SelectedItems(0).Index)
    End Sub

    Private Sub BO_ADD_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_ADD_LIGNE.Click

        ' utilisateur autorisé à ajouter ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        ' creation 
        ClearTextBoxes(Me)
        ' Ajouter un nouveau tarif
        szSQL = "CALL spUPD_LETTRE('NEW','" & nq(TE_LIBELLE.Text) & "','" & nq(TE_CORPS.Text) & "');"
        If ExecQuery(ctDSN_DEVFAC, szSQL) Then
            ' réaffiche la viewlist
            InitListeLettres(LV_LETTRES, 0)
            ' ouvrir le fomulaire de modif
            LV_LETTRES_DoubleClick(sender, e)
        End If

    End Sub

    Private Sub BO_DELETE_LIGNE_Click(sender As System.Object, e As System.EventArgs) Handles BO_DELETE_LIGNE.Click

        ' utilisateur autorisé à supprimer ?
        If IsUserAuthorised(9) = False Then
            Exit Sub
        End If
        deleteEnregistrement("ARTICLES", LV_LETTRES.SelectedItems(0).SubItems(1).Text, LV_LETTRES.SelectedItems(0).Text)
        ' Cacher les détails
        BO_ANNULER_Click(sender, e)

    End Sub

 
    Private Sub LV_LETTRES_DoubleClick(sender As Object, e As System.EventArgs) Handles LV_LETTRES.DoubleClick

        If LV_LETTRES.SelectedItems.Count > 0 Then
            LettretoScreen(LV_LETTRES.SelectedItems(0).Text)
            ' afficher les détails de la formule
            TC_Details.Visible = True
        End If

    End Sub

    Private Sub LV_LETTRES_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles LV_LETTRES.KeyDown

        Select Case e.KeyCode
            ' touche Enter = DoubleClick
            Case Keys.Enter
                LV_LETTRES_DoubleClick(sender, e)

            Case Keys.Insert
                BO_ADD_LIGNE_Click(sender, e)

            Case Keys.Delete
                BO_DELETE_LIGNE_Click(sender, e)

        End Select

    End Sub

    Private Sub LettretoScreen(ByVal ref As String)

        ' Lire l'enreg sélectionné
        szSQL = "CALL spGET_LETTRE('" & ref & "')"
        GetRecordset(ctDSN_DEVFAC, szSQL, rs)
        If rs.RecordCount > 0 Then
            TE_REF.Text = rs.Fields("ref").Value
            TE_LIBELLE.Text = rs.Fields("libelle").Value
            TE_CORPS.Text = rs.Fields("corps").Value
        End If
        rs.Close()

    End Sub


End Class